#include "utils/utils.h"

#include <iostream>
#include <libconfig.h++>
#include <exception>

using namespace std;
using namespace libconfig;



string loadConfigParams(string ConfigFile, string Command)
{
	string ReadCommand="";
	Config cfg;
	// Read the file. If there is an error, report it and exit.
	try
	{
		cout << "ConfigFile:" << ConfigFile.c_str() << endl;
		cfg.readFile(ConfigFile.c_str());
		cfg.lookupValue(Command, ReadCommand);
	}
	catch (const libconfig::FileIOException &e){
		cerr << "Error while found file." << endl;
	}
	catch (const libconfig::ParseException & e) {
		cerr << "Error while found file." << endl;
	}
	catch(const exception & e){
		cerr << "Error while reading file." << endl;
	}
	return(ReadCommand);
}

string exec(const char* cmd)
{
	FILE* pipe = popen(cmd, "r");
	if (!pipe) return("ERROR");
	char buffer[128];
	string result = "";
	while(!feof(pipe)) {
		if(fgets(buffer, 128, pipe) != NULL)
			result += buffer;
	}
	pclose(pipe);
	return(result);
}
